//
//  MeetingViewController.m
//  OneToOne
//
//  Created by ByteDance on 2023/2/20.
//

#import "MeetingViewController.h"
@interface MeetingViewController () <SRWebSocketDelegate, socketDelegate, VideoH264DecoderDelegate>
@property (nonatomic, strong) VideoSession *videoSession;
@property (nonatomic, strong) AudioSession *audioSession;
@property (nonatomic, strong) SRWebSocket *socket;

@property (nonatomic, strong) MetalView *otherView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) VideoH264Decode *decode;
@end

@implementation MeetingViewController

- (void)viewDidLoad {
    self.index = 0;
    [super viewDidLoad];
    [self otherView];
    [self initVideo];
    [self initAudio];
    [self socket];
    [self decode];
}
- (void)initVideo {
    [self videoSession];
}
- (void)initAudio {
    [self audioSession];
}
#pragma mark - SRWebSocketDelegate
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
    NSLog(@"connected");
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
    if ([message isKindOfClass:NSMutableData.class]) {
        NSMutableData *data = (NSMutableData *)message;
        [self.decode decodeNalu:(uint8_t *)data.bytes size:(uint32_t)data.length];
    }
}

- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
    NSLog(@"connection failed: %@", error);
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"connection closed: %@, %@", @(code), reason);
}

#pragma mark - socketDelegate

- (void)sendVideo:(NSData *_Nullable)videoData {
    [self.socket sendData:videoData error:nil];
}

- (void)sendAudio:(NSData *_Nullable)audioData {
    [self.socket sendData:audioData error:nil];
}
#pragma mark - VideoH264DecoderDelegate
- (void)decoder:(VideoH264Decode *) decoder didDecodingFrame:(CVImageBufferRef) imageBuffer {
    //[self.otherView putInBuffer:imageBuffer];
    UIImageOrientation orientation =
        UIImageOrientationRight;
    UIImage *image = [UIUtilities UIImageFromImageBuffer:imageBuffer orientation:orientation];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.image = image;
    });
}
#pragma mark - getter
- (MetalView *)otherView {
    if (!_otherView) {
//        _otherView = [[MetalView alloc] init];
//        [self.view addSubview:[_otherView getView]];
//        [_otherView getView].frame = self.view.frame;
        self.imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
        self.imageView.layer.borderColor = UIColor.whiteColor.CGColor;
        self.imageView.layer.borderWidth = 2.0;
        [self.view addSubview:self.imageView];
    }
    return _otherView;
}

- (VideoSession *)videoSession {
    if (!_videoSession) {
        _videoSession = [[VideoSession alloc] init];
        _videoSession.delegate = self;
        UIView *localView = [[UIView alloc] initWithFrame:CGRectMake( 50, - 150, self.view.frame.size.width, self.view.frame.size.height)];
        [self.view addSubview:localView];
        [_videoSession bindToWindow:localView];
    }
    return _videoSession;
}

- (AudioSession *)audioSession {
    if (!_audioSession) {
        _audioSession = [[AudioSession alloc] init];
        _audioSession.delegate = self;
    }
    return _audioSession;
}
- (SRWebSocket *)socket {
    if (!_socket) {
        NSURL *url = [NSURL URLWithString:@"ws://192.168.43.62:8183"];
        _socket = [[SRWebSocket alloc] initWithURL:url];
        _socket.delegate = self;
        [_socket open];
    }
    return _socket;
}


- (VideoH264Decode *)decode {
    if (!_decode) {
        _decode = [[VideoH264Decode alloc] init];
        _decode.delegate = self;
    }
    return _decode;
}
@end
